package com.sjh.lib_app.statusMananger.defWidget;

import android.animation.ValueAnimator;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.widget.ImageView;

import java.lang.ref.WeakReference;

/**
 * explain : 加载动画工具
 * author : SunJH
 * data : 2020/4/3
 */
public class LoadingAnimator {
    private static float[] mCurrentPosition = new float[2];

    public static void circleAnimation(float radiut, WeakReference<ImageView> weakReferenceImage) {
        Path path = new Path();
        path.addCircle(0, 0, radiut, Path.Direction.CW);
        final PathMeasure mPathMeasure = new PathMeasure(path, true);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, mPathMeasure.getLength());
        valueAnimator.setDuration(2000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ImageView imageView = weakReferenceImage.get();
                if(imageView != null){
                    float value = (Float) animation.getAnimatedValue();
                    mPathMeasure.getPosTan(value, mCurrentPosition, null);
                    imageView.setTranslationX(mCurrentPosition[0]);
                    imageView.setTranslationY(-mCurrentPosition[1]);
                }else{
                    valueAnimator.cancel();
                }
            }
        });
        valueAnimator.start();
    }
}

